package com.party.mobile.web.controller.photo;

import com.google.common.collect.Maps;
import com.party.authorization.annotation.Authorization;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.member.PartnerMember;
import com.party.core.model.photo.PhotoProject;
import com.party.core.model.system.SysConfig;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IPartnerMemberService;
import com.party.core.service.photo.IPhotoProjectService;
import com.party.core.service.system.ISysConfigService;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.login.output.CurrentUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 行知公社---我的相册
 * @Date: 2019/11/13 9:36
 */
@RestController
@RequestMapping(value = "my/photos")
public class PhotoController {

    @Autowired
    private IPhotoProjectService photoProjectService;
    @Autowired
    private CurrentUserBizService currentUserBizService;
    @Autowired
    private IPartnerMemberService partnerMemberService;
    @Autowired
    private IMemberGroupService memberGroupService;
    @Autowired
    private ISysConfigService sysConfigService;

    Logger logger = LoggerFactory.getLogger(getClass());

    @Authorization
    @RequestMapping("list")
    public AjaxResult list(HttpServletRequest request, Page page) {

        try {
            //获取当前登录会员
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
//            获取行知公社参数配置
            SysConfig xzgsConfig = sysConfigService.findByCode("XZGSID");
            String partnerId = "";
            if (xzgsConfig == null) {
                //正式环境中行知公社机构ID
                partnerId = "c94e89415d7d4611b706cecfc43af2ae";
            } else {
                partnerId = xzgsConfig.getValue();
            }
            List<String> orgIds = memberGroupService.getChildWithMy(partnerId);
            List<String> partenrIds = new ArrayList<>();
            PartnerMember mainOrg = partnerMemberService.getMainOrg(currentUser.getId(), partnerId);
            if (mainOrg != null) {
                partenrIds.add(mainOrg.getPartnerId());
            }

            Map<String, Object> params = Maps.newHashMap();
            if (partenrIds.contains(partnerId) && partenrIds.size() ==1) {
                params.put("orgIds",orgIds);
            } else {
                params.put("orgIds",partenrIds);
            }

            List<PhotoProject> photoProjectList = photoProjectService.webListPage(params, page);

            return AjaxResult.success(photoProjectList,page);
        } catch (Exception e) {
            logger.error("行知公社获取我的相册异常",e);
            return AjaxResult.error(100,"行知公社获取我的相册异常");
        }
    }

}
